use actix_web::{App, HttpResponse, HttpServer, web};
use std::io;
#[derive(Debug)]
pub struct AppState {
    pub health_check_response: String,
    pub visit_count: std::sync::Mutex<u32>,
}
#[actix_web::main]
pub async fn main() -> io::Result<()> {
    unsafe {
        std::env::set_var("RUST_LOG", "debug");
        env_logger::init();
    }

    let state = web::Data::new(AppState {
        health_check_response: "I'm ok".to_string(),
        visit_count: std::sync::Mutex::new(0),
    });
    HttpServer::new(move || {
        App::new()
            .app_data((state.clone()))
            .route("/health", web::get().to(health_check))
    })
    .bind("127.0.0.1:3000")?
    .run()
    .await
}
// #[get="/health"]
pub async fn health_check(state: web::Data<AppState>) -> HttpResponse {
    let health_check_response = &state.health_check_response;
    let mut visit_count = state.visit_count.lock().unwrap();
    let response = format!("{} {}times", health_check_response, visit_count);
    *visit_count += 1;
    HttpResponse::Ok().json(response)
}
